package cn.onesdream.app.api.controller;

import cn.onesdream.app.api.service.FriendService;
import cn.onesdream.app.common.util.ResponseResult;
import cn.onesdream.app.dao.entity.Friend;
import cn.onesdream.app.dao.entity.User;
import cn.onesdream.app.dao.mapper.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author WangShiyu
 * @date 2021/3/13 12:24
 */
@RestController
@RequestMapping("/api/friend")
@Slf4j
@Api(tags = "友邻相关接口")
@Validated
public class FriendController {
    @Autowired
    private FriendService friendService;

    @ApiOperation("删除友邻")
    @PostMapping("/deleteFriend")
    public ResponseResult<Friend> deleteFriend(Integer friendUserId){
        Integer userId = ((User) SecurityContextHolder.getContextHolderStrategy().getContext().getAuthentication().getPrincipal()).getId();
        friendService.deleteFriend(userId, friendUserId);
        return new ResponseResult<>();
    }

    @ApiOperation("通过UserId获取友邻")
    @GetMapping("/getFriend/{userId}")
    public ResponseResult<List> getFriend(@PathVariable("userId") @NotNull Integer userId){
        List<Friend> friendList = friendService.getFriend(userId);
        return new ResponseResult<>(friendList);
    }

    @ApiOperation("添加友邻")
    @PostMapping("/")
    public ResponseResult<Friend> addFriend(Integer friendUserId){
        Integer userId = ((User) SecurityContextHolder.getContextHolderStrategy().getContext().getAuthentication().getPrincipal()).getId();
        friendService.addFriend(userId, friendUserId);
        return new ResponseResult<>();
    }
}
